package cn.ljy.mes.service.impl;

import cn.ljy.authority.config.security.UserUtil;
import cn.ljy.authority.dao.mapper.SysFileResourceMapper;
import cn.ljy.authority.model.SysFileResource;
import cn.ljy.authority.service.impl.IBaseService;
import cn.ljy.common.exception.model.MyWebException;
import cn.ljy.mes.dao.mapper.MesCarModelConfigMapper;
import cn.ljy.mes.model.MesCarModelConfig;
import cn.ljy.mes.model.ShopCarProduct;
import cn.ljy.mes.model.query.ShopCarProductQuery;
import cn.ljy.mes.service.ShopCarProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class IShopCarProductService extends IBaseService<ShopCarProduct, ShopCarProductQuery> implements ShopCarProductService {
    @Autowired
    private SysFileResourceMapper fileResourceMapper;
    @Autowired
    private MesCarModelConfigMapper carModelConfigMapper;

    @Override
    protected void enrichEntityFields(ShopCarProduct shopCarProduct){
        if(shopCarProduct!=null){
            //处理封面图片的绝对网址
            SysFileResource coverImage=shopCarProduct.getCoverImage();
            if(coverImage!=null){
                //获取资源的绝对网址
                coverImage.setFileHttpUrl(super.getFileResourceWebUrl(coverImage.getFileUrl()));
            }
        }
    }

    @Override
    protected void beforeSaveOrUpdate(ShopCarProduct shopCarProduct) {
        if(shopCarProduct.getCarModelConfig()!=null){//只有传递值过来才进行判断
            if(shopCarProduct.getCarModelConfig().getId()!=null){
                MesCarModelConfig carModelConfig = carModelConfigMapper.findByIdNoRelation(shopCarProduct.getCarModelConfig().getId());
                if(carModelConfig==null){
                    throw new MyWebException("验证失败：汽车配置不存在");
                }
            }else{
                throw new MyWebException("验证失败：车型配置id不能为空");
            }
        }
        if(shopCarProduct.getCoverImage()!=null) {//只有传递值过来才进行判断
            if(shopCarProduct.getCoverImage().getId()==null){
                throw new MyWebException("验证失败：封面图片对应的资源id不能为空");
            }else{
                //读取系统记录
                SysFileResource coverImage = fileResourceMapper.findByIdNoRelation(shopCarProduct.getCoverImage().getId());
                if(coverImage==null){
                    throw new MyWebException("验证失败：所选资源不存在");
                }
            }
        }
    }

    @Override
    protected void beforeSave(ShopCarProduct shopCarProduct) {
        //设置当前添加的用户信息
        shopCarProduct.setCreator(UserUtil.getLoginUser());
    }

    @Override
    protected void beforeUpdate(ShopCarProduct shopCarProduct) {
        //获取系统原纪录
        ShopCarProduct carProductOfOriginal=mapper.findByIdNoRelation(shopCarProduct.getId());
        if(carProductOfOriginal==null){
            throw new MyWebException("验证失败：记录不存在");
        }
        //验证失败：促销价格不能大于原价
        BigDecimal price = shopCarProduct.getPrice() != null ? shopCarProduct.getPrice() : carProductOfOriginal.getPrice();
        BigDecimal originalPrice = shopCarProduct.getOriginalPrice() != null ? shopCarProduct.getOriginalPrice() : carProductOfOriginal.getOriginalPrice();
        if (price != null && originalPrice != null && price.compareTo(originalPrice) == 1) {
            throw new MyWebException("验证失败：促销价格不能大于原价");
        }
        //设置当前编辑的用户信息
        shopCarProduct.setEditor(UserUtil.getLoginUser());
    }


}
